<template>
  <div class="user-home">
    <el-row :gutter="20">
      <!-- 欢迎卡片 -->
      <el-col :span="24">
        <el-card class="welcome-card">
          <div class="welcome-content">
            <h2>欢迎使用药店管理系统</h2>
            <p>{{ greeting }}</p>
          </div>
        </el-card>
      </el-col>

      <!-- 快捷入口 -->
      <el-col :span="24" style="margin-top: 20px">
        <h3 class="section-title">快捷入口</h3>
        <el-row :gutter="20" class="quick-links">
          <el-col :xs="12" :sm="8" :md="6">
            <el-card class="quick-link-card" @click="navigateTo('/user/medicine')">
              <el-icon size="30"><FirstAidKit /></el-icon>
              <div class="quick-link-title">药品浏览</div>
            </el-card>
          </el-col>

          <el-col :xs="12" :sm="8" :md="6">
            <el-card class="quick-link-card" @click="navigateTo('/user/cart')">
              <el-icon size="30"><ShoppingCart /></el-icon>
              <div class="quick-link-title">购物车</div>
            </el-card>
          </el-col>

          <el-col :xs="12" :sm="8" :md="6">
            <el-card class="quick-link-card" @click="navigateTo('/user/order')">
              <el-icon size="30"><List /></el-icon>
              <div class="quick-link-title">我的订单</div>
            </el-card>
          </el-col>

          <el-col :xs="12" :sm="8" :md="6">
            <el-card class="quick-link-card" @click="navigateTo('/user/profile')">
              <el-icon size="30"><User /></el-icon>
              <div class="quick-link-title">个人中心</div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>

      <!-- 药品推荐 -->
      <el-col :span="24" style="margin-top: 20px">
        <h3 class="section-title">药品推荐</h3>
        <el-row :gutter="20" class="medicine-list">
          <el-col
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            v-for="(item, index) in recommendedMedicines"
            :key="index"
          >
            <el-card class="medicine-card" @click="viewMedicineDetail(item.id)">
              <!-- <div class="medicine-image">
                <el-image 
                  :src="item.image || 'https://via.placeholder.com/150x150?text=药品图片'" 
                  fit="cover"
                  :alt="item.name"
                ></el-image>
              </div> -->
              <div class="medicine-info">
                <h4 class="medicine-name">{{ item.name }}</h4>
                <div class="medicine-price">¥{{ item.price }}</div>
                <div class="medicine-type">{{ item.type }}</div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { FirstAidKit, ShoppingCart, List, User } from '@element-plus/icons-vue'
import { listMedicine } from '@/api/medicine'

const router = useRouter()

// 用户信息
const userInfo = ref({})

// 欢迎语
const greeting = computed(() => {
  const hour = new Date().getHours()
  let timeGreeting = ''

  if (hour < 6) {
    timeGreeting = '凌晨好'
  } else if (hour < 9) {
    timeGreeting = '早上好'
  } else if (hour < 12) {
    timeGreeting = '上午好'
  } else if (hour < 14) {
    timeGreeting = '中午好'
  } else if (hour < 17) {
    timeGreeting = '下午好'
  } else if (hour < 19) {
    timeGreeting = '傍晚好'
  } else {
    timeGreeting = '晚上好'
  }

  return `${timeGreeting}，${userInfo.value.username || '用户'}，欢迎回来！`
})

// 推荐药品列表
const recommendedMedicines = ref([])

// 获取用户信息
onMounted(() => {
  const userInfoStr = localStorage.getItem('userInfo')
  if (userInfoStr) {
    try {
      userInfo.value = JSON.parse(userInfoStr)
    } catch (error) {
      console.error('解析用户信息失败', error)
    }
  }

  // 获取推荐药品
  getRecommendedMedicines()
})

// 获取推荐药品
const getRecommendedMedicines = async () => {
  try {
    const res = await listMedicine({ pageSize: 8 })
    recommendedMedicines.value = res || []
  } catch (error) {
    console.error('获取推荐药品失败', error)
    // 模拟数据
    recommendedMedicines.value = [
      { id: 1, name: '阿司匹林肠溶片', price: 15.8, type: '解热镇痛药', image: '' },
      { id: 2, name: '布洛芬缓释胶囊', price: 22.5, type: '解热镇痛药', image: '' },
      { id: 3, name: '感冒灵颗粒', price: 12.8, type: '感冒药', image: '' },
      { id: 4, name: '板蓝根颗粒', price: 10.5, type: '清热解毒药', image: '' },
      { id: 5, name: '维生素C片', price: 18.0, type: '维生素类', image: '' },
      { id: 6, name: '复方甘草片', price: 9.5, type: '镇咳药', image: '' },
      { id: 7, name: '藿香正气水', price: 16.8, type: '解表药', image: '' },
      { id: 8, name: '牛黄解毒片', price: 25.0, type: '清热解毒药', image: '' }
    ]
  }
}

// 导航到指定路由
const navigateTo = (path) => {
  router.push(path)
}

// 查看药品详情
const viewMedicineDetail = (id) => {
  router.push(`/user/medicine/detail/${id}`)
}
</script>

<style scoped>
.user-home {
  padding: 20px 0;
}

.welcome-card {
  background-color: #409eff;
  color: white;
}

.welcome-content {
  padding: 20px;
}

.welcome-content h2 {
  margin-top: 0;
  margin-bottom: 10px;
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 15px;
  color: #303133;
}

.quick-links {
  margin-bottom: 20px;
}

.quick-link-card {
  height: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
}

.quick-link-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.quick-link-title {
  margin-top: 10px;
  font-size: 16px;
}

.medicine-list {
  margin-bottom: 20px;
}

.medicine-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.medicine-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.medicine-image {
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.medicine-image .el-image {
  width: 100%;
  height: 100%;
}

.medicine-info {
  padding: 10px;
}

.medicine-name {
  margin: 0 0 10px 0;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.medicine-price {
  color: #f56c6c;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.medicine-type {
  color: #909399;
  font-size: 14px;
}
</style>
